package ldh.im.fxgui;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import ldh.im.fxbase.util.FxThreadUtil;
import ldh.im.fxbase.util.StageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public abstract class TrayApplication extends Application {

    private static final Logger LOGGER = LoggerFactory.getLogger(TrayApplication.class);

    private java.awt.TrayIcon trayIcon;

    @Override
    public void start(Stage primaryStage) {
        if (StageUtil.isSupportTray()) {
            Platform.setImplicitExit(false); //多次使用显示和隐藏设置false
//            LOGGER.info("start tray!!!");
        } else {
            //系统托盘不支持
//            LOGGER.info(Thread.currentThread().getStackTrace()[ 1 ].getClassName() + ":系统托盘不支持");
        }

        StageUtil.setMainStage(primaryStage);
        StageUtil.setHostServices(this.getHostServices());

        startPane(primaryStage);
    }

    protected abstract void startPane(Stage primaryStage);

    protected void close() {
        if (StageUtil.isSupportTray()) {
            Platform.setImplicitExit(true);
        }
        SystemTray tray = SystemTray.getSystemTray();
        tray.remove(trayIcon);
        Platform.exit();
    }

    protected void enableTray(final Stage stage)  {
        MouseListener sj = new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {

                    FxThreadUtil.runLater(new Runnable() {
                        @Override
                        public void run() {
//                            StageUtil.getStage().show();
//                            StageUtil.getStage().setIconified(false);
//                            StageUtil.getStage().toFront();

                            MainUi mainUi = new MainUi();
                        }
                    });
                }
            }
        };

        try {
            SystemTray tray = SystemTray.getSystemTray();

            JPopupMenu popupMenu2 = new JPopupMenu();
            int fontSize = 16;
//            JMenuItem settingMenu = new JMenuItem("设置");
//            settingMenu.setPreferredSize(new Dimension(fontSize * 8, fontSize * 2 ));
////            settingMenu.setIcon(new ImageIcon(ImageUtil.resizeTrayPng(getClass().getResourceAsStream("/assets/settings.png"), iconSize, iconSize)));
//            settingMenu.addActionListener(e-> {
//                System.out.println("设置");
//            });
//            popupMenu2.add(settingMenu);

            //退出菜单
            JMenuItem exitMenu = new JMenuItem("退出");
            exitMenu.setPreferredSize(new Dimension(fontSize * 8, fontSize * 2 ));
            exitMenu.addActionListener(e->{
                FxThreadUtil.runLater(()->{
                    StageUtil.getMainStage().close();
                });
                close();
            });
            popupMenu2.add(exitMenu);
            trayIcon = new SystemTrayIcon(ImageIO.read(getClass().getResourceAsStream("/imgs/icon.jpg")), "宇连通IM", popupMenu2);
            trayIcon.setToolTip("宇连通IM");
            tray.add(trayIcon);
            trayIcon.addMouseListener(sj);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
